<?php

/**
 * 充值管理控制器
 */
class PayAction extends UserAction {

    public $model;

    public function _initialize() {
        parent::_initialize();
        $this->model = D("Order");
    }
    
    /**
     * 账户信息 
     */
    public function account(){
        $account = M('UserAccount')->find(session('uid'));
        $this->assign('account', $account);
        $this->display();
    }

    /**
     * 充值中心 
     */
    public function index() {
        //金币充值比
        $money2gold = C('MONEY_TO_GOLD');
        //推荐信息
        $roundDZ    = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('money2gold', $money2gold);
        $this->display();
    }

    public function done() {
        $uid = session('uid');
        $money = format_int($_REQUEST['money']*100);
        if ($uid == false )
            $this->error('用户未登录');

        $model  = M('Order');
        $userid = intval($uid);
        if ($userid) {
            $user = M('User')->find($userid);
            if (!$user) {
                $this->assign('jumpUrl', U('User/Public/login'));
                $this->error('用户未登录');
            }
        }

        /* 检查充值参数 */
        if($money <= 0){
            $this->error('充值金额输入有误');
        }
        
        
        $order = array();
        /* 充值金额 */
        $amount = $money;
        
        /*充值可转换的金币数*/
        $order['to_glod'] = C('MONEY_TO_GOLD')*$amount;
        
        /* 支付方式 */
        $paymentid = intval($_POST['payment']);
        $Payment   = M('Payment')->find($paymentid);

        $order['amount'] = $amount;
        $order['order_amount'] = $order['amount'];

        /* 发票 */
        if ($Payment['pay_fee']) {
            $order['pay_fee']      = $Payment['pay_fee_type'] ? $Payment['pay_fee'] : $order['order_amount'] * $Payment['pay_fee'] / 100;
            $order['pay_fee']      = $order['pay_fee']*100;
        }
        $order['order_amount'] = $order['order_amount'] + $order['pay_fee'];

        $order['userid']          = intval($userid);
        $order['status']          = 0;
        $order['pay_status']      = 0;
        $order['shipping_status'] = 0;

        $order['pay_id']      = intval($Payment['id']);
        $order['pay_name']    = $Payment['pay_name'] ? $Payment['pay_name'] : '';
        $order['pay_code']    = $Payment['pay_code'] ? $Payment['pay_code'] : '';
        $order['postmessage'] = htmlspecialchars($_POST['postmessage']);
        
        $order['add_time'] = time();
        foreach ($order as $key => $r) {
            if ($r == null)
                $order[$key] = '';
        }
        
        $orderid     = $model->add($order);
        if ($orderid) {
            $order['sn'] = date("Ymd") . sprintf('%06d', $orderid);
            $model->save(array('id' => $orderid, 'sn' => $order['sn']));
  
            if ($order['pay_id']) {
                if ($order['pay_code'] == 'Balance') {
                    $this->error('支付方式有误');
                } else {
                    $pay_code                      = $order['pay_code'];
                    $aliapy_config                 = unserialize($Payment['pay_config']);
                    $aliapy_config['order_sn']     = $order['sn'];
                    $aliapy_config['order_amount'] = format_price($order['order_amount']);
                    $aliapy_config['body']         = $order['consignee'] . ' ' . $order['postmessage'];
                    import("Think.ORG.Pay." . $pay_code);
                    $pay                           = new $pay_code($aliapy_config);
                    $paybutton                     = $pay->get_code();
                    $this->assign('paybutton', $paybutton);
                }
            }
        }

        $this->assign('order', $order);
        $this->assign('money', $money);
        $this->display();
    }

}
